package com.zkdcloud.repository;

import com.zkdcloud.domain.Business;
import org.jfaster.mango.annotation.DB;
import org.jfaster.mango.annotation.SQL;
import org.jfaster.mango.plugin.page.Page;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * businessDao
 *
 * @author zk
 * @since 2018/5/2
 */
@DB(table = "business")
@Repository
public interface BusinessDao {
    String column = " id,name,phone,address,pic_url,status,opening_time,shipping_fee,package_fee,min_price," +
            "shipping_time,month_sales,bulletin,category,booking_times,create_time ";

    /**
     * 添加business
     *
     * @param business b
     */
    @SQL("insert into #table(" + column + ") values(:id,:name,:phone,:address,:picUrl,:status,:openingTime,:shippingFee,:packageFee,:minPrice," +
            ":shippingTime,:monthSales,:bulletin,:category,:bookingTimes,:createTime)")
    void addBusiness(Business business);

    /**
     * 查询所有商家
     *
     * @param page page
     * @return business
     */
    @SQL("select * from #table order by create_time desc")
    List<Business> getAll(Page page);

    /**
     * 根据id获取
     *
     * @param id id
     * @return b
     */
    @SQL("select * from #table where id = :1")
    Business getOne(String id);

    /**
     * 根据id删除
     *
     * @param id id
     */
    @SQL("delete from #table where id = :1")
    void remove(String id);

    /**
     * 更新指定商家
     *
     * @param business b
     */
    @SQL("update #table set id = :id " +
            " #if(:name != null), name = :name #end" +
            " #if(:phone != null), phone = :phone #end" +
            " #if(:address != null), address = :address #end" +
            " #if(:picUrl != null), pic_Url = :picUrl #end" +
            " #if(:status != null) ,status = :status #end" +
            " #if(:openingTime != null) ,opening_Time = :openingTime #end" +
            " #if(:shippingFee != null), shipping_Fee = :shippingFee #end" +
            " #if(:packageFee != null) ,package_Fee = :packageFee #end" +
            " #if(:minPrice != null), min_Price = :minPrice #end" +
            " #if(:shippingTime != null), shipping_Time = :shippingTime #end" +
            " #if(:bulletin != null), bulletin = :bulletin #end" +
            " #if(:category != null), category = :category #end" +
            " #if(:monthSales != null), month_sales = :monthSales #end" +
            " #if(:bookingTimes != null), booking_Times = :bookingTimes #end" +
            " where id = :id")
    void update(Business business);
}
